<?php

declare (strict_types=1);

namespace app\controller\admin;

use think\facade\Db;
use think\facade\View;
use think\facade\Request;

class Article extends Common
{
    public function index($act = null, $cid = null)
    {
        if ($act == 'del') {
            if (!Request::isPost()) {
                return error(lang('参数错误'));
            }
            $aids = input('post.');
            if ($aids) {
                Db::name('article')->where('aid', 'in', $aids['aids'])->delete();
                add_log('删除文章，AID：'.implode(',', $aids['aids']), $this->request->user->username);
                return success(lang('操作成功'), (string) url('admin/article/index'));
            }
            return error(lang('请勾选需要删除的选项'));
        }

        $cid = intval($cid);
        if ($cid) {
            $where = "a.cid = '{$cid}'";
        } else {
            $where = '1=1';
        }
        $list = Db::name('article')->alias('a')->join('category c', 'a.cid=c.id')->field('a.*,c.name')->where($where)->order('a.aid desc')->paginate(25);
        View::assign('list', $list);
        return View::fetch();
    }

    public function add()
    {
        $category = Db::name('category')->field('id,pid,name')->order('o asc')->select()->toArray();
        $category = categorize_and_sort_hierarchy($category);
        View::assign('category', $category);

        return View::fetch();
    }

    public function edit($aid = null)
    {
        $aid = intval($aid);
        $article = Db::name('article')->where(['aid' => $aid])->find();
        if (!$article) {
            return error(lang('参数错误'));
        }
        $category = Db::name('category')->field('id,pid,name')->order('o asc')->select();
        $category = categorize_and_sort_hierarchy($category);
        View::assign('category', $category);
        View::assign('article', $article);
        return View::fetch();
    }

    public function save($aid = null)
    {
        if (!Request::isPost()) {
            return error(lang('参数错误'));
        }
        $aid = intval($aid);
        $cid = input('post.cid', 0, 'intval');
        if (!$cid) {
            return error(lang('参数错误'));
        }
        $title = input('post.title');
        $keywords = input('post.keywords');
        $description = input('post.description');
        $image = input('post.image');
        $t = time();
        $stick = input('post.stick', 0, 'intval');
        $content = input('post.content');
        if (!$aid) {
            $aid = Db::name('article')->insertGetId(['cid' => $cid,'title' => $title,'keywords' => $keywords,'description' => $description,'image' => $image,'t' => $t,'stick' => $stick,'content' => $content]);
            if (!$aid) {
                return error(lang('系统错误'));
            }
            add_log('新增文章，AID：'.$aid, $this->request->user->username);
            return success(lang('操作成功'), (string) url('admin/article/index'));
        } else {
            Db::name('article')->where(['aid' => $aid])->update(['cid' => $cid,'title' => $title,'keywords' => $keywords,'description' => $description,'image' => $image,'t' => $t,'stick' => $stick,'content' => $content]);
            add_log('编辑文章，AID：'.$aid, $this->request->user->username);
            return success(lang('操作成功'), (string) url('admin/article/index'));
        }
    }
}
